package com.lyn.tencent_health_code.config;

import com.lyn.tencent_health_code.database.User;
import com.lyn.tencent_health_code.service.EncrypDESService;
import com.lyn.tencent_health_code.service.UserService;
import com.lyn.tencent_health_code.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;

/**
 * @author almost
 */
@Configuration
public class AdminConfig {
    public static final int GENERAL_USER = 0;
    public static final int ADMINISTRATOR = 1;


    private Logger logger = LoggerFactory.getLogger(AdminConfig.class);

    @Bean
    public void init() {
        UserService userService = SpringUtils.getBean(UserService.class);
        EncrypDESService encoder = SpringUtils.getBean(EncrypDESService.class);
        if (userService.count() == 0) {
            User user = new User();
            user.setId(UUID.randomUUID().toString());
            user.setUserName("admin");
            user.setUserPassword(encoder.encrypt("123456"));
            user.setTime(System.currentTimeMillis());
            user.setType(ADMINISTRATOR);
            User save = userService.save(user);
            if (save != null) {
                logger.info("admin Initialization successful");
            } else {
                logger.info("admin Initialization failure");
            }

        }
    }
}
